﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Blog.Models;
using Blog.Models.UserServices;
using Spirit.Portal.Core.Web;
using SubSonic.Repository;

namespace Blog.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public IUserRole _re1 = new UserRoleRepository();
        public IUserRepository _re;
        public HomeController()
        {
            _re=new UserRepository();
        }
        public HomeController(IUserRepository re)
        {
            _re = re;
        }
        IUserService UserService=new UserService();
        public ActionResult Index()
        {
            ViewData["color"] = "#F0F";
            return View();
        }
        public ActionResult About()
        {
            return View();
        }
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(AccountLoginModel model)
        {
            if (model.Password != null && model.Username != null)
            {
                LoginResult result = UserService.Login(model.Username, model.Password);
                if (!result.IsSuccess)
                {
                    ModelState.AddModelError("Username", "Login Fall");
                }
            }

            if (ModelState.IsValid)
            {
                    LoginResult result = UserService.Login(model.Username, model.Password);

                    if (result.IsSuccess)
                    {
                        Session.SetPrincipal(new Principal
                        {
                            Roles = result.Roles,
                            Username = result.User.UserName
                        });
                        var url = Request.QueryString["r"];

                        if (string.IsNullOrEmpty(url))
                            return RedirectToRoute( new { controller = "Home", action = "Index" });

                        return Redirect(url);
                    }
                }

            return View(model);
        }
        public ActionResult LogOff()
        {
            var x = Session.GetPrincipal().Username;
            _re.GetDataByName(Session.GetPrincipal().Username).Lastvisit=DateTime.Now;
            _re.Save();
            Session.SetOffline();
            return RedirectToAction("Index");
        }
        public ActionResult Register()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Register([Bind(Exclude = "Id")]RegisterModel model)
        {
                if (string.IsNullOrEmpty(model.Password))
                {
                    ModelState.AddModelError("Password", "Chưa điền thông tin");
                }
                else
                {
                    if(model.Password.Length<6||model.Password.Length>10)
                    ModelState.AddModelError("Password", "Mật khẩu từ 6 đến 20 kí tự");
                }
                if (model.ConfirmPassword!=model.Password)
                {
                    ModelState.AddModelError("", "pass and confirm must math");
                }
                if (_re.GetDataByName(model.UserName)!=null)
                 {
                  ModelState.AddModelError("UserName", "Please choose another username.This name is already exit");
                 }
                if(ModelState.IsValid)
                {
                    var c = new User();
                    c.UserName = model.UserName;
                    c.FullName = model.FullName;
                    c.PassWord = model.Password;
                    c.Phone = model.Phone;
                    c.Email = model.Email;
                    c.ArticleCount = 0;
                    c.Lastvisit = DateTime.Now;
                    _re.Add(c);
                    _re.Save();
                    var x = new UserRole();
                    x.RoleId = 3;
                    x.UserId = _re.GetDataByName(model.UserName).UserId;
                    _re1.Add(x);
                    _re1.Save();
                    LoginResult result = UserService.Login(model.UserName, model.Password);

                    if (result.IsSuccess)
                    {
                        Session.SetPrincipal(new Principal
                        {
                            Roles = result.Roles,
                            Username = result.User.UserName
                        });
                        var url = Request.QueryString["r"];

                        if (string.IsNullOrEmpty(url))
                            return RedirectToRoute(new { controller = "Home", action = "Index" });

                        return Redirect(url);
                    }
                }
            return View(model);
                
            }

    }
}
